package edu.gatech.bsbmule.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

import edu.gatech.bsbmule.model.Game;
import edu.gatech.bsbmule.model.Mule;
import edu.gatech.bsbmule.model.Player;
import edu.gatech.bsbmule.model.Store;


/**
 * @author bsb
 * This class will iplement the IStoreScreen interface
 *
 */
public class StoreScreen extends JPanel implements IStoreScreen  {
	private JLabel label;
	private JComboBox<String> resourceList; 
	private JButton buyButton;
	private JButton sellButton; 
	private JSpinner spinnerQuantity; 
	private JButton returnButton; 
	private static StoreScreen instance;
	
	public static StoreScreen getInstance() {
		if (instance == null) {
			instance = new StoreScreen();
		}
		return instance;
	}
	

	/**
	 * Create the panel.
	 */
	public StoreScreen() {
		setBorder(new EmptyBorder(20,20,20,20));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		label = new JLabel();
		label.setSize(448,150);
		this.setLabel(Store.getInstance().getResources());

		resourceList = new JComboBox<String>();
		resourceList.setFont(new Font("Tahoma", Font.PLAIN, 16));
		resourceList.setModel(new DefaultComboBoxModel(new String[]{"Select Item:", "Food", "Energy", "Smithore","Food Mule (+25)",
				"Energy Mule (+50)", "Smithore Mule (+75)"}));
		resourceList.setSize(330, 116); 
		
		spinnerQuantity = new JSpinner();
		spinnerQuantity.setFont(new Font("Tahoma", Font.PLAIN, 24));
		spinnerQuantity.setModel(new SpinnerNumberModel(0, 0, 150, 1));
		spinnerQuantity.setSize(118, 116); 
		
		buyButton = new JButton("Buy"); 
		buyButton.setSize(118, 100); 
		buyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectedResource = StoreScreen.getInstance().getSelectedResource(); 
				int quantity = StoreScreen.getInstance().getQuantity(); 
				Player currentPlayer = Game.getInstance().getCurrentPlayer();

				//if the quantity is zero and there is no selected resource
				if (quantity == 0 || selectedResource == -1) {
					System.out.println("No quantity/item selected."); 
				}
				//if the selected resource is food, energy, or smithore 
				else if (selectedResource == Store.RESOURCE_FOOD || 
						selectedResource == Store.RESOURCE_ENERGY ||
						selectedResource == Store.RESOURCE_SMITHORE) {
					int finalPrice = Store.getInstance().getPrice(selectedResource); //gets the price of the resource
					finalPrice = finalPrice * quantity; //calculates total price 
					if(Store.getInstance().hasMoney(currentPlayer, finalPrice) //check to see if player has money and if there is enough quantity in the store
							&& quantity <= Store.getInstance().getItemQuantity(selectedResource)){
						Store.getInstance().sellResource(currentPlayer, selectedResource, quantity); //sells resource
						StoreScreen.getInstance().setLabel(Store.getInstance().getResources()); //resets the label with updated quantities
						StoreScreen.getInstance().reset(); //sets everything back to default values
						
						//print updated info to console
						System.out.println(currentPlayer.getName() + " has purchased " + quantity + " " + 
						Store.getInstance().getResourceName(selectedResource) + " for " + finalPrice);
						System.out.println(currentPlayer.getInventory());
					}
					else {
						System.out.println("You don't have enough money"); //user didn't have enough money 
					}
				}
				//this process if for buying a mule. similar to above
				else if (selectedResource == Store.FOOD_MULE ||
						selectedResource == Store.ENERGY_MULE ||
						selectedResource == Store.SMITHORE_MULE) {
					int finalPrice = Store.getInstance().getMulePrice(selectedResource);
					finalPrice = finalPrice * quantity; 
					if(Store.getInstance().hasMoney(currentPlayer, finalPrice) 
							&& quantity <= Store.getInstance().getItemQuantity(0)){
						Store.getInstance().sellResource(currentPlayer, selectedResource, quantity); 
						StoreScreen.getInstance().setLabel(Store.getInstance().getResources());
						StoreScreen.getInstance().reset(); 
						System.out.println("You bought a mule");
						currentPlayer.setMule(selectedResource);
					}
					else {
						System.out.println("You don't have enough money"); 
					}
					
				}
			}
		});
		
		
		// This part deals with the selling.
		sellButton = new JButton("Sell");
		sellButton.setSize(224, 100);
		sellButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		
			int selectedResource = StoreScreen.getInstance().getSelectedResource(); 
			int quantity = StoreScreen.getInstance().getQuantity(); 
			Player currentPlayer = Game.getInstance().getCurrentPlayer();
		
			if (quantity == 0 || selectedResource == -1) {
				System.out.println("No quantity/item selected."); 
			}
			else if (selectedResource == Store.RESOURCE_FOOD || 
					selectedResource == Store.RESOURCE_ENERGY ||
					selectedResource == Store.RESOURCE_SMITHORE) {
		           // we have to check that the player has a quantity of these resources which is greater than 
	               // the quantity that we are trying to sell. 
				
			        	if ( selectedResource==Store.RESOURCE_FOOD){
					       if ( quantity>= currentPlayer.getFood())
					         {
						       System.out.println("Quantity not available");
					         }
					    else{
						     // we calculate the money earned from selling this quantity of food
						     int moneyEarned = Store.getInstance().getPrice(selectedResource);
						     currentPlayer.addMoney(moneyEarned);
						     //print updated info to console					
						     System.out.println("money: "+ currentPlayer.getMoney());
						     currentPlayer.subtractFood( quantity);
						     System.out.println(" quantity left: "+ currentPlayer.getFood());

						     // update the player gui
						  
					        }
				}
                if ( selectedResource==Store.RESOURCE_ENERGY ){
                	if ( quantity>= currentPlayer.getEnergy())
                	{
                		System.out.println("Quantity not available");
                	}
                	else{
						  // we calculate the money earned from selling this quantity of energy
						  int moneyEarned = Store.getInstance().getPrice(selectedResource);
						  currentPlayer.addMoney(moneyEarned);
						  System.out.println("money: "+ currentPlayer.getMoney());
						  currentPlayer.subtractEnergy(quantity);
						  System.out.println(" quantity left: "+ currentPlayer.getEnergy());
						  // update the player gui/						  
					  }
                }
                if ( selectedResource==Store.RESOURCE_SMITHORE ){
                    if (quantity>= currentPlayer.getSmithore())
                	{
                		System.out.println("Quantity not available");
                	}
                	else{
						  // we calculate the money earned from selling this quantity of smithore
						  int moneyEarned = Store.getInstance().getPrice(selectedResource);
						  currentPlayer.addMoney(moneyEarned);
						  System.out.println("money: "+ currentPlayer.getMoney());
						  currentPlayer.subtractSmithore(quantity);
						  System.out.println(" quantity left : "+ currentPlayer.getSmithore());
						  // update the player gui
						  
					  }
                }
			}
			else if (selectedResource == Store.FOOD_MULE ||
					selectedResource == Store.ENERGY_MULE ||
					selectedResource == Store.SMITHORE_MULE) {
				
		        // check to see if the player has a mule and the mule type is the same as the one selected
				
				Mule mule = currentPlayer.getMule();
				int muleType = mule.getMuleType();
				if (mule==null){
					System.out.println("player does not have a mule to sell");
				}
				else{
					    if ( selectedResource==Store.FOOD_MULE)
					    {
					    	if ( mule.getMuleType()==4){
					    		
					    	 int moneyEarned  =  Store.getInstance().getMulePrice(selectedResource);
					    	 currentPlayer.addMoney(moneyEarned);
					    	 System.out.println("money: "+ currentPlayer.getMoney());
					    	 
					    	 currentPlayer.setMule(-1);
					    	 // update player gui
					    		
					    	}
					    	else{
					    		System.out.println("player does not have this type of mule");
					    	}
					    	
					    }
					    
					    
					    if ( selectedResource==Store.ENERGY_MULE)
					    {
					    	if ( mule.getMuleType()==5){
					    		
					    	 int moneyEarned  =  Store.getInstance().getMulePrice(selectedResource);
					    	 currentPlayer.addMoney(moneyEarned);
					    	 System.out.println("money: "+ currentPlayer.getMoney());
					    	 currentPlayer.setMule(-1);
					    	 // update player gui
					    		
					    	}
					    	else{
					    		System.out.println("player does not have this type of mule");
					    	}
					    	
					    }
					    
					    
					    if ( selectedResource==Store.SMITHORE_MULE)
					    {
					    	if ( mule.getMuleType()==6){
					    		
					    	 int moneyEarned  =  Store.getInstance().getMulePrice(selectedResource);
					    	 currentPlayer.addMoney(moneyEarned);
					    	 System.out.println("money: "+ currentPlayer.getMoney());
					    	 currentPlayer.setMule(-1);
					    	 // update player gui
					    		
					    	}
					    	else{
					    		System.out.println("player does not have this type of mule");
					    	}
					    	
					    }
					    
					    
				}
			
			}
			}
				
				
							
			});			
				
			
				
			
                
                
                


		//button to return back to the town 
		returnButton = new JButton("Return to Town"); 
		returnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TownWindow.getInstance().setTitle("The Town");
				TownWindow.getInstance().setPanel(TownScreen.getInstance());
			}
		});
		this.add(label); 
		this.add(resourceList);
		this.add(spinnerQuantity); 
		this.add(buyButton);
		this.add(sellButton); 
		this.add(returnButton); 
	}
	//this creates a chart with the quantities of each item. use this to update after transation
	public void setLabel(int[][] resources) {
		label.setText("<html><b>Item    |    Quantity   </b>"+ "<b>    |     Price</b><br>" 
					+ "------------------------"+ "---------------<br>"
					+ "Food    |    Quantity: " + resources[1][1] + "    |     Price: 30 <br>"
					+ "Energy  |    Quantity: " + resources[2][1] + "    |     Price: 25 <br>"
					+ "Smithore|    Quantity: " + resources[3][1] + "    |     Price: 50 <br>"
					+ "Mule    |    Quantity: " + resources[0][1] + "    |     Price: 100 <br></html>");
	}
	//gets the quantity the user wants
	public int getQuantity() {
		return (Integer) spinnerQuantity.getValue(); 
	}
	//gets the resource the user wants
	public int getSelectedResource() {
		int selectedItem = resourceList.getSelectedIndex(); 
		if (selectedItem == 1) {
			return Store.RESOURCE_FOOD;
		}
		else if (selectedItem == 2) {
			return Store.RESOURCE_ENERGY;
		}
		else if (selectedItem == 3) {
			return Store.RESOURCE_SMITHORE;
		}
		else if (selectedItem == 4) {
			return Store.FOOD_MULE;
		}
		else if (selectedItem == 5) {
			return Store.ENERGY_MULE;
		}
		else if (selectedItem == 6) {
			return Store.SMITHORE_MULE;
		}
		else {
			return -1; //if at "select item"
		}
	}
	//resets the spinner and combo box to original values 
	public void reset() {
		resourceList.setSelectedIndex(0);
		spinnerQuantity.setValue(0);
		
	}


}
